package org.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import org.dk.domain.DataResult;
import org.dk.em.DataResultEnum;
import org.dk.pojo.TbContent;
import org.dk.service.ContentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("contentController")
public class ContentController {

    @Reference
    private ContentService contentService;

    @RequestMapping("selectRoundImg")
    @ResponseBody
    public DataResult selectRoundImg(){
        /*String imgRedis = ConstantUtils.IMG_REDIS;
        String IMG_ROUND = ConstantUtils.IMG_ROUND;*/
        List<TbContent> list;
        /*if(redisTemplate.opsForHash().hasKey(imgRedis,IMG_ROUND)){
            list = (List<TbContent>)redisTemplate.opsForHash().get(imgRedis,IMG_ROUND);
            return Result.buildResult(ResultEnum.SUCCESS).setData(list);
        }*/

        try {
            list = contentService.selectByCateId();
            /*redisTemplate.opsForValue().set(IMG_ROUND,list);*/
            return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            e.printStackTrace();
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }
    }

    @RequestMapping("recommend")
    @ResponseBody
    public DataResult recommend(){
        /*String IMG_RECOMMEND = ConstantUtils.IMG_RECOMMEND;
        String imgRedis = ConstantUtils.IMG_REDIS;*/
        List<TbContent> list;
        /*if(redisTemplate.opsForHash().hasKey(imgRedis,IMG_RECOMMEND)){
            list = (List<TbContent>)redisTemplate.opsForHash().get(imgRedis,IMG_RECOMMEND);
            return Result.buildResult(ResultEnum.SUCCESS).setData(list);
        }*/

        try {
            list = contentService.selectRecommend();
            /*redisTemplate.opsForValue().set(IMG_RECOMMEND,list);*/
            return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            e.printStackTrace();
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }
    }

    @RequestMapping("favourite")
    @ResponseBody
    public DataResult favourite(){
       /* String IMG_FAVOURITE = ConstantUtils.IMG_FAVOURITE;
        String imgRedis = ConstantUtils.IMG_REDIS;*/
        List<TbContent> list;
       /* if(redisTemplate.opsForHash().hasKey(imgRedis,IMG_FAVOURITE)){
            list = (List<TbContent>)redisTemplate.opsForHash().get(imgRedis,IMG_FAVOURITE);
            return Result.buildResult(ResultEnum.SUCCESS).setData(list);
        }*/
        try {
            list = contentService.favourite();
            /*redisTemplate.opsForValue().set(IMG_FAVOURITE,list);*/
            return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            e.printStackTrace();
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }
    }
}
